package org.batatasframework.support.database.values;

import org.batatasframework.support.database.sql.field.SelectFragment;

public abstract class AbstractValueHolder implements FieldValueHolder{
	
	protected final SelectFragment sourceFragment;
	
	public AbstractValueHolder(SelectFragment sourceFragment){
		this.sourceFragment = sourceFragment;
	}
	
	public abstract class AbstractValue<T> implements FieldValue{
		
		protected final T value;
		
		public AbstractValue(T value){
			this.value = value;
		}
		
		@Override
		public Object sourceValue() {
			return value;
		}
		
		@Override
		public String toString() {
			if(null == value) return "";
			return value.toString();
		}
	}
//	
//	public static ValueHolder instance(FieldElement field, Object format){
////		FieldMetadata fm = fieldRelation.getFieldMetadata();
//		//Object format = fm.getFormat();
////		String alias = fieldRelation.getAlias();
//		if(format instanceof OptionsFormat){
//			return new OptionsValueHolder(field, alias, (OptionsFormat)format);
//		}
//		int type = field.getDataType();
//		ValueHolder value;
//		switch (type) {
//		case Types.VARCHAR:
//			value = new StringValueHolder(field, alias, format);
//			break;
//		case Types.INTEGER:
//		case Types.BIGINT:
//		case Types.SMALLINT:
//			value = new NumberValueHolder(field, alias);
//			break;
//		case Types.DOUBLE:
//		case Types.FLOAT:
//			value = new DecimalValueHolder(field, alias, format);
//			break;
//		case Types.DATE:
//			value = new DateValueHolder(field, alias, format);
//			break;
//		case Types.TIME:
//			value = new DateValueHolder(field, alias, format){
//				@Override
//				public IValue read(ResultSet rs) throws Exception {
//					return new DateValue(field, rs.getTime(alias), format);
//				}
//			};
//			break;
//		case Types.TIMESTAMP:
//			value = new DateValueHolder(field, alias, format){
//				@Override
//				public IValue read(ResultSet rs) throws Exception {
//					return new DateValue(field, rs.getTimestamp(alias), format);
//				}
//			};
//			break;
//		case Types.BOOLEAN:
//		case Types.BIT:
//			value = new BooleanValueHolder(field, alias, field.getFormat());
//			break;
//		default:
//			value = new ObjectValueHolder(field, alias);
//			break;
//		}
//		return value;
//	}
}
